//
//  Network+Download.h
//  YanXiuWang
//
//  Created by 慧趣小歪 on 16/12/15.
//  Copyright © 2016年 小分队. All rights reserved.
//

#import "Network.h"

@class ResponseDownload, RequestDownload;
typedef void (^OnDownloadFinish)(ResponseDownload * _Nonnull response);
typedef RequestDownload * _Nonnull (^NET_DOWN_URL)(NSString * _Nonnull url);
typedef RequestDownload * _Nonnull (^NET_DOWN_PROGRESS)(NET_PROGRESS_BLOCK _Nonnull progressBlock);
typedef RequestDownload * _Nonnull (^NET_DWON_TEXT)(NSString * _Nonnull string);

#pragma mark - 下载事务队列
@interface NetworkDownloadQueue : NetworkQueue <NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>

@property (nonnull, readonly, nonatomic) NSURLSession *downloadSession;

@property (nonnull, readonly, nonatomic) NET_DOWN_URL download;

@end

#pragma mark - 下载
@interface NetworkManager (Download)

@property (nonnull, readonly, nonatomic) NetworkDownloadQueue *downloadQueue;

@property (nonnull, readonly, nonatomic) NET_DOWN_URL download;

@property (readonly, nonatomic) NSUInteger diskCacheSize;
- (void)clearDisk;
- (NSUInteger)fileSizeByURL:(NSString * _Nonnull)url;

+ (nonnull NSString *)makeDiskCachePath:(NSString * _Nonnull)fullNamespace;
+ (nonnull NSString *)bookmarkPathForKey:(NSString * _Nonnull)key;
+ (nonnull NSString *)cachedFileNameForKey:(NSString * _Nonnull)key;

@end

#pragma mark - 下载响应结果
@interface ResponseDownload : ResponseNormal

@property (nonatomic, strong) NSURL * _Nullable location;
@property (nonatomic, assign) long long localSize;
@property (nonatomic, assign) long long totalSize;

@property (nonatomic, readonly) double progressPercent;
@property (nonatomic, readonly) NSProgress * _Nonnull progress;

// 取消下载
- (void)cancel;

@end


#pragma mark - 下载请求
@interface RequestDownload : RequestNormal

/*  下载到指定Path @param path(String)  */
@property (nonatomic, readonly, nonnull) NET_DWON_TEXT to;
/*  下载进度@param progress(UIProgress) */
@property (nonatomic, readonly, nonnull) NET_DOWN_PROGRESS progress;


- (nonnull ResponseDownload *)onFinish:(OnDownloadFinish _Nonnull)onFinishBlock;

@end
